package cn.iocoder.yudao.module.erp.controller.admin.business.money.moneyincome;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.erp.controller.admin.business.money.moneyincome.vo.MoneyIncomePageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.business.money.moneyincome.vo.MoneyIncomeRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.business.money.moneyincome.vo.MoneyIncomeSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.business.money.moneyincome.MoneyIncomeDO;
import cn.iocoder.yudao.module.erp.service.business.money.moneyincome.MoneyIncomeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;

@Tag(name = "管理后台 - 收入类型")
@RestController
@RequestMapping("/erp/money-income")
@Validated
public class MoneyIncomeController {

    @Resource
    private MoneyIncomeService moneyIncomeService;

    @PostMapping("/create")
    @Operation(summary = "创建收入类型")
    @PreAuthorize("@ss.hasPermission('erp:money-income:create')")
    public CommonResult<Long> createMoneyIncome(@Valid @RequestBody MoneyIncomeSaveReqVO createReqVO) {
        return success(moneyIncomeService.createMoneyIncome(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新收入类型")
    @PreAuthorize("@ss.hasPermission('erp:money-income:update')")
    public CommonResult<Boolean> updateMoneyIncome(@Valid @RequestBody MoneyIncomeSaveReqVO updateReqVO) {
        moneyIncomeService.updateMoneyIncome(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除收入类型")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:money-income:delete')")
    public CommonResult<Boolean> deleteMoneyIncome(@RequestParam("id") Long id) {
        moneyIncomeService.deleteMoneyIncome(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得收入类型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:money-income:query')")
    public CommonResult<MoneyIncomeRespVO> getMoneyIncome(@RequestParam("id") Long id) {
        MoneyIncomeDO moneyIncome = moneyIncomeService.getMoneyIncome(id);
        return success(BeanUtils.toBean(moneyIncome, MoneyIncomeRespVO.class));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得收入类型精简信息")
    @PreAuthorize("@ss.hasPermission('erp:money-income:query')")
    public CommonResult<List<MoneyIncomeRespVO>> getMoneyIncomeList() {
        MoneyIncomePageReqVO moneyIncomePageReqVO = new MoneyIncomePageReqVO();
        moneyIncomePageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        PageResult<MoneyIncomeDO> moneyIncomePage = moneyIncomeService.getMoneyIncomePage(moneyIncomePageReqVO);
        return success(convertList(moneyIncomePage.getList(), item -> new MoneyIncomeRespVO().setId(item.getId())
                .setName(item.getName())));
    }

    @GetMapping("/page")
    @Operation(summary = "获得收入类型分页")
    @PreAuthorize("@ss.hasPermission('erp:money-income:query')")
    public CommonResult<PageResult<MoneyIncomeRespVO>> getMoneyIncomePage(@Valid MoneyIncomePageReqVO pageReqVO) {
        PageResult<MoneyIncomeDO> pageResult = moneyIncomeService.getMoneyIncomePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MoneyIncomeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出收入类型 Excel")
    @PreAuthorize("@ss.hasPermission('erp:money-income:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMoneyIncomeExcel(@Valid MoneyIncomePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MoneyIncomeDO> list = moneyIncomeService.getMoneyIncomePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "收入类型.xls", "数据", MoneyIncomeRespVO.class,
                        BeanUtils.toBean(list, MoneyIncomeRespVO.class));
    }

}